/**
 * Copyright (c) 2016-2021 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.zsmartsystems.zigbee.dongle.xbee.internal.protocol;

import java.util.HashMap;
import java.util.Map;

/**
 * Class to implement the XBee Enumeration <b>ModemStatus</b>.
 * <p>
 * Note that this code is autogenerated. Manual changes may be overwritten.
 *
 * @author Chris Jackson - Initial contribution of Java code generator
 */
public enum ModemStatus {
    /**
     * Default unknown value
     */
    UNKNOWN(-1),

    /**
     * [0]
     */
    HARDWARE_RESET(0x0000),

    /**
     * [1]
     */
    WATCHDOG_TIMER_RESET(0x0001),

    /**
     * [2]
     */
    JOINED_NETWORK(0x0002),

    /**
     * [3]
     */
    DISASSOCIATED(0x0003),

    /**
     * [6]
     */
    COORDINATOR_STARTED(0x0006),

    /**
     * [7]
     */
    NETWORK_SECURITY_KEY_UPDATED(0x0007),

    /**
     * [13]
     */
    VOLTAGE_SUPPLY_LIMIT_EXCEEDED(0x000D),

    /**
     * [17]
     */
    MODEM_CONFIGURATION_CHANGED_WHILE_JOIN_IN_PROGRESS(0x0011);

    /**
     * A mapping between the integer code and its corresponding type to
     * facilitate lookup by code.
     */
    private static Map<Integer, ModemStatus> codeMapping;

    private int key;

    private ModemStatus(int key) {
        this.key = key;
    }

    private static void initMapping() {
        codeMapping = new HashMap<Integer, ModemStatus>();
        for (ModemStatus s : values()) {
            codeMapping.put(s.key, s);
        }
    }

    /**
     * Lookup function based on the type code. Returns null if the code does not exist.
     *
     * @param modemStatus
     *            the code to lookup
     * @return enumeration value.
     */
    public static ModemStatus getModemStatus(int modemStatus) {
        if (codeMapping == null) {
            initMapping();
        }

        if (codeMapping.get(modemStatus) == null) {
            return UNKNOWN;
        }

        return codeMapping.get(modemStatus);
    }

    /**
     * Returns the XBee protocol defined value for this enum
     *
     * @return the XBee enumeration key
     */
    public int getKey() {
        return key;
    }
}
